/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.com.oceansoft.modules.handyconsult.controller;

import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.framework.kits.ApiResult;
import cn.com.oceansoft.modules.base.entity.ApiQueryBase;
import cn.com.oceansoft.modules.cache.service.BaseCacheService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @author cms
 * @version 2016-04-10
 */
@Controller
@RequestMapping(value = "handyconsult")
@Api(value = "便民咨询", description = "便民咨询展现页面")
public class HandyConsultController {
    @Resource
    protected ConfigInfo configInfo;
    @Resource
    private BaseCacheService cacheService;

    /**
     * 便民咨询
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "consult", method = GET)
    public String gotomoduleIndex(HttpServletRequest request, Model model) {
        try {
            //加载新闻
            String xwResult = cacheService.get("index:xwResult");
            if (StringUtils.isEmpty(xwResult)) {
                xwResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/page",
                        "ps", 7, "pi", 1, "kw", "", "sb", "public_time", "sd", "desc", "q_deleted_s_eq", "N");
                cacheService.set("index:xwResult", xwResult, 120);
            }
            ApiResult xwList = JSONObject.parseObject(xwResult, ApiResult.class);
            model.addAttribute("xwList", xwList);

            //加载公示公告
            String gsggResult = cacheService.get("index:gsggResult");
            if (StringUtils.isEmpty(gsggResult)) {
                gsggResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/gsgg",
                        "ps", 7, "pi", 1, "kw", "gsgg", "sb", "public_time", "sd", "desc", "q_deleted_s_eq", "N");
                cacheService.set("index:gsggResult", gsggResult, 600);
            }
            ApiResult gsggList = JSONObject.parseObject(gsggResult, ApiResult.class);
            model.addAttribute("gsggList", gsggList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "handy_consult/consultCenter";
    }

    /**
     * 咨询列表（分页）
     *
     * @param queryBase
     * @param request
     * @return
     */
    @RequestMapping(value = "page", method = GET)
    @ResponseBody
    public ApiResult page(ApiQueryBase queryBase, HttpServletRequest request,Model model) throws Exception {
        String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "consults/page", request.getParameterMap(), "UTF-8");
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }

    /**
     * 我要咨询
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = {"myconsult"})
    public String reportProtocol(HttpServletRequest request, Model model) {
        return "handy_consult/consultCenter-myConsult";
    }

    @RequestMapping(method = POST)
    @ResponseBody
    public ApiResult post(@RequestBody Map consult, HttpServletRequest request) {
        String result = HttpClientUtil.doPostString(configInfo.getMsHostUrl() + "consults", consult);
        if (StringUtils.isEmpty(result)) {
            return new ApiResult(false, 10400, "服务器异常", null);
        }
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }

    @RequestMapping(value = "{id}", method = GET)
    @ResponseBody
    public ApiResult get(@PathVariable String id, HttpServletRequest request) {
        String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "consults/" + id);
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }
}